package common

import (
	"context"
	"encoding/json"
	"sale-admin/internal/app/grpc/client"
	"sale-admin/internal/app/grpc/protoc/mini_app"
	"sale-admin/internal/app/grpc/protoc/system_admin"
	"sale-admin/internal/app/web/schema"

	"192.168.1.75/go-pkg/errorx"
	"192.168.1.75/go-pkg/logx"
	"github.com/samber/lo"
)

type Order struct{}

// 订单商品老到新  兼容之前的字段，取出时转新的字段
func (c Order) CountDataOldToNew(countData string) (data []schema.OrderGoodsDetail) {
	var OldCountData []schema.OrderGoodsDetailOld
	json.Unmarshal([]byte(countData), &OldCountData)
	if len(OldCountData) > 0 && OldCountData[0].GoodsID != "" {
		var CountData []schema.OrderGoodsDetail
		for _, v := range OldCountData {
			v1 := schema.OrderGoodsDetail(v)
			v1.GoodsName, _ = UnicodeDecode(v1.GoodsName)
			CountData = append(CountData, v1)
		}
		return CountData
	} else {
		var CountData []schema.OrderGoodsDetail
		json.Unmarshal([]byte(countData), &CountData)
		return CountData
	}
}

// 订单商品新到老  兼容之前的字段，存入时转老版本字段
func (c Order) CountDataNewToOld(countData string) (data []schema.OrderGoodsDetailOld) {
	var NewCountData []schema.OrderGoodsDetail
	json.Unmarshal([]byte(countData), &NewCountData)
	if len(NewCountData) > 0 && NewCountData[0].GoodsID != "" {
		var CountData []schema.OrderGoodsDetailOld
		for _, v := range NewCountData {
			v1 := schema.OrderGoodsDetailOld(v)
			v1.GoodsName, _ = UnicodeEncode(v1.GoodsName)
			CountData = append(CountData, v1)
		}
		return CountData
	} else {
		var CountData []schema.OrderGoodsDetailOld
		json.Unmarshal([]byte(countData), &CountData)
		return CountData
	}
}

// 获取所有子商户
func (c Order) GetMerchantChildren(ctx context.Context, merchantID string, selectedMerchants []string) (merchants []string, err error) {
	//获取所有子节点
	var req system_admin.MerchantListReq
	req.MerchantId = merchantID
	SystemAdminClient, clientErr := client.GetSystemAdminClient()
	if clientErr != nil {
		err = clientErr
		return
	}
	// merchantID = 1 时查询所有商户
	merchantList, merchantErr := SystemAdminClient.MerchantList(ctx, &req)
	if merchantErr != nil {
		err = errorx.New("获取商户信息错误", -1)
		return
	}
	childMerchants := []string{}
	for _, v := range merchantList.List {
		childMerchants = append(childMerchants, v.MerchantId)
	}
	if len(selectedMerchants) == 1 && selectedMerchants[0] == "" {
		merchants = childMerchants
		if merchantID != "1" {
			merchants = append(merchants, merchantID)
		}
	} else if len(selectedMerchants) > 0 {
		merchants = lo.Intersect(selectedMerchants, childMerchants)
	} else {
		merchants = childMerchants
		if merchantID != "1" {
			merchants = append(merchants, merchantID)
		}
	}
	return
}

// OrderStatusNotify 订单状态变化通知
func (Order) OrderStatusNotify(ctx context.Context, outTradeNo string, orderStatus int) error {
	miniAppCli, err := client.GetMiniAppClient()
	if err != nil {
		logx.Error(context.Background(), "获取小程序Grpc客户端失败")
		return err
	}
	rsp, err := miniAppCli.OrderStatusNotify(ctx, &mini_app.OrderStatusNotifyReq{OutTradeNo: outTradeNo, OrderStatus: int32(orderStatus)})
	if err == nil && rsp != nil && rsp.Code == 0 {
		return nil
	}
	return errorx.New("订单完成通知失败", -1)
}
